#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

//string的构造
void Test3()
{
    //构造方式一
    //string s1 = "hello world";
    //char* p = "hello world";
    //s1(p);
    string s2("hello world");
    cout << s2 << endl;
     
    string s3(10,'$');
    for(size_t i = 0;i < s3.size();i++)
    {
        cout << s3[i];
    }
    cout << endl;

    string s4(s3);
    cout << s4 << endl; 
}

//迭代器的使用
void Test4()
{
    //正向迭代器
    string s = "hello";
    string::iterator it = s.begin();
    while(it != s.end())
    {
        cout << *it;
        ++it;
    }
    cout << endl;
    //反向迭代器
    string::reverse_iterator re_it = s.rbegin();
    while(re_it != s.rend())
    {
        cout << *re_it;
        re_it++;
    }
    cout << endl;
    
}


//string类+=的运算
void Test1()
{
    string s = "hello";
    s += " ";
    s += "world";
    cout << s << endl;
}

//string类迭代器的使用
void Test2()
{
    string s = "hello";
    
}

//容量相关
void Test5()
{
    string s("hello");
    cout << s.size() << endl;
    cout << s.capacity() << endl;
    //cout << sizeof(string) << endl;
    if(s.empty())
    {
        cout << "true:空" << endl;
    }
    else 
    {
        cout << "false:不空" << endl;
    }
    
    s.clear();
    if(s.empty())
    {
        cout << "true:空" << endl;
    }
    else 
    {
        cout << "false:不空" << endl;
    }
}

//resize()
void Test6()
{
    //resize改变实效大小
    string s("hello");
    cout << s.size() << endl;//5
    cout << s.capacity() << endl; //5

    s.resize(10,'!');
    cout << s.size() << endl; //10
    cout << s.capacity() << endl;//10
    cout << s << endl;

    s.resize(20,'$');
    cout << s.size() << endl;//20
    cout << s.capacity() << endl;//20
    cout << s << endl;

    s.resize(50,'@');
    cout << s.size() << endl;// 50
    cout << s.capacity() << endl;//50
    cout << s << endl;

   s.resize(40);
   cout << s.size() << endl;// 40
   cout << s.capacity() << endl;//50
   cout << s << endl;

   s.resize(20);
   cout << s.size() << endl;// 20
   cout << s.capacity() << endl;//50
   cout << s << endl;

}

//void reserve(size_t newcapacity)
void Test7()
{
    string s("hello");
    //cout << s[100] << endl;
    //cout << s.at(100) << endl;
    cout << s.size() << endl;//5
    cout << s.capacity() << endl;//10
    cout << s << endl;

    s.reserve(10);
    cout << s.size() << endl;//5
    cout << s.capacity() << endl;//10
    cout << s << endl;

    s.reserve(20);
    cout << s.size() << endl;//5
    cout << s.capacity() << endl;//20
    cout << s << endl;

    s.reserve(50);
    cout << s.size() << endl;//5
    cout << s.capacity() << endl;//50
    cout << s << endl;

    s.reserve(20);
    cout << s.size() << endl;//5
    cout << s.capacity() << endl;//20
    cout << s << endl;

    s.reserve(10);
    cout << s.size() << endl;//5
    cout << s.capacity() << endl;//10
    cout << s << endl;

}

void Test8()
{
    string s;
    s += "hello";
    s += " ";

    string s1("world");
    s += s1;
    cout << s << endl;
    s = s.append(3,'!');
    s = s.append(s1,1,3);
    cout << s << endl;

}

//insert
void Test9()
{
    string s("hello");
    s.insert(0,"world ");
    cout << s << endl;
    s.insert(4,3,' ');
    cout << s << endl;
}

//erase
void Test10()
{
    string s("hello world");
    s.erase(0,5);
    cout << s <<endl;// world
    s.erase(s.begin());
    cout << s <<endl;//world
    s.erase(s.begin(),s.end());
    cout << s <<endl;//
}

void Test11()
{
    string s("12345");
    int value = atoi(s.c_str());
    cout << value << endl;
}

//获取file的后缀
void Test12()
{
    string file("string.cpp");
    //substr是所给位置到末尾
    string postFix = file.substr(file.find('.')+1);
    cout << postFix << endl;

    //获取文件名字
    string file1("F:\\work\\string.cpp");

    size_t pos1 = file1.rfind('\\') + 1;
    size_t pos2 = file1.rfind('.') - 1;

    string name = file1.substr(pos1,pos2-pos1+1);
    cout << name << endl;
}

//验证扩容机制
//两倍的扩增
void Test13()
{
    string s;
    s.reserve(90);
    size_t oldcapacity = s.capacity();

    for(size_t i = 0; i < 100; i++)
    {
        s.push_back('a');
        if(oldcapacity != s.capacity())
        {
            cout << s.capacity() << endl;
            oldcapacity = s.capacity();
        }
    }
}

void Test14()
{
    string s;
    getline(cin,s);
    cout << s <<endl;
}

int main()
{
    //Test1();
    //Test3();
    //Test4();  
    Test14();
    return 0;
}
